package com.cskaoyan.javase.thread._1extends;

import java.io.*;

/**
 * 启动两个线程分别用字符流和字节流，对一个文件同时进行文件复制操作。
 *
 * 给下载加一个"进度条"
 * 当下载xxx时,在控制台输出当前下载的总进度%
 * 被复制的文件的大小是可以获得
 * 然后一次性复制的大小也是确定的,进行累加
 * 除以总的文件大小 就得到了复制的进度
 *
 * @since 16:18
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Exercise {
    public static void main(String[] args) {
        DownloadThread downloadThread = new DownloadThread();
        downloadThread.start();

        DownloadThread2 downloadThread2 = new DownloadThread2();
        downloadThread2.start();
    }

}

class DownloadThread extends Thread {
    @Override
    public void run() {
        // 循环读循环写
        try (FileInputStream in = new FileInputStream("abc.txt"); FileOutputStream out = new FileOutputStream("acopy1.txt")) {
            int readData;
            while ((readData = in.read()) != -1) {
                out.write(readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


class DownloadThread2 extends Thread {
    @Override
    public void run() {
        // 循环读循环写
        try (FileWriter writer = new FileWriter("acopy2.txt"); FileReader reader = new FileReader("a.txt")) {
            int readData;
            while ((readData = reader.read()) != -1) {
                writer.write(readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}